<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class Student extends Model{

    /**
     * 获取所有学生信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $student = $this
            ->alias('stu')
            ->join('user u', 'stu.student_id=u.id', 'LEFT')
            ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
            ->join('grade gra', 'stu.grade_id=gra.id', 'LEFT')
            ->field('stu.id,u.full_name,stu.tuition,stu.coin,
                    stu.student_card_id,stu.student_id,stu.follow_les_id,
                    rank.student_rank,gra.grade')
            ->where($where)
            ->select();
        if($student){
            foreach($student as $k => $v){
                $student[$k] = $v->toArray();
            }
        }

        return $student;
    }

    /**
     * 获取所有学生记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount($where=[]){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('stu')
            ->join('user u', 'stu.student_id=u.id', 'LEFT')
            ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
            ->join('grade gra', 'stu.grade_id=gra.id', 'LEFT')
            ->field('stu.id,u.full_name,u.username,u.nickname,
                    stu.tuition,stu.coin,stu.student_card_id,stu.student_id,
                    stu.follow_les_id,rank.student_rank,gra.grade ')
            ->where($where)
            ->count();
        return $count;
    }

    /**
     * 查询单条信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=""){
        $info = $this
            ->alias('stu')
            ->join('user u', 'stu.student_id=u.id', 'LEFT')
            ->join('grade gra','stu.grade_id = gra.id','LEFT')
            ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
            ->field('stu.id,u.full_name,u.username,u.nickname,u.tel,
                    u.sex,stu.tuition,stu.coin,stu.student_card_id,stu.student_id,
                    stu.follow_les_id,stu.student_rank_id,rank.student_rank,
                    stu.grade_id,gra.grade')
            ->where($where)
            ->find();
            if($info){
                $info = $info -> toArray();
            }
        return $info;
    }

    /**
     * 通过teacher表中id查询
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getStudentById($id){

        $where = "stu.id=".$id;

        $info = $this
            ->alias('stu')
            ->join('user u', 'stu.student_id=u.id', 'LEFT')
            ->join('grade gra','stu.grade_id = gra.id','LEFT')
            ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
            ->field('stu.id,u.full_name,u.username,u.nickname,u.tel,
                    u.sex,stu.tuition,stu.coin,stu.student_card_id,stu.student_id,
                    stu.follow_les_id,stu.student_rank_id,rank.student_rank,
                    stu.grade_id,gra.grade')
            ->where($where)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }

    /**
     * 通过student表中student_id查询单条信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getStudentByStudentId($id){
        $info = $this
            ->alias('stu')
            ->join('user u', 'stu.student_id=u.id', 'LEFT')
            ->join("coin c",'stu.student_id = c.receive_id','LEFT')
            ->join('grade gra','stu.grade_id = gra.id','LEFT')
            ->join('address add','stu.student_id = add.member_id','LEFT')
            ->join('area area','add.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
            ->field('stu.id,u.full_name,u.username,u.nickname,u.tel,u.user_images,u.rec_bonus,
                    stu.tuition,stu.coin,stu.student_card_id,stu.student_id,
                    stu.follow_les_id,stu.student_rank_id,rank.student_rank,
                    stu.grade_id,gra.grade,add.address,area.name as area_name,
                    area.citycode,city.name as city_name,city.provincecode,
                    pro.name as province_name,c.month_coin')
            ->where('stu.student_id='.$id)
            ->find();
        if($info){
            $info = $info -> toArray();
            if($info['follow_les_id'] != ''){
                $follow_lesson = "";
                $lesson_ids = explode(',',$info['follow_les_id']);
                foreach($lesson_ids as $k => $v){
                    $lesson = Db::name('lesson')->where('id',$v)->find();
                    $follow_lesson .= $lesson['lesson_name'].'、';
                }
                $info['follow_les_id'] = trim($follow_lesson,'、');
            }

        }
        return $info;
    }
}